package com.sfzd5.UI;

import com.sfzd5.aiclassroom.modle.Question;
import com.sfzd5.exam.helplibrary.Generator;
import com.sfzd5.exam.helplibrary.taskstatisitcs.QuestionInfo;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.text.StyleContext;
import java.awt.*;
import java.util.Locale;

public class QuestionKnowledgePanel {
    private JLabel lb_knowledge;
    private JTextPane htmlPane;
    public JPanel mPanel;

    private Question q;

    public QuestionKnowledgePanel(Question question) {
        this.q = question;
        htmlPane.setText(generateQuestionHtml(Generator.generateQuestionHtml_forPanelOnlyQuestion(q)));
        reSetKnowledge();
    }

    public void reSetKnowledge() {
        if (q.knowledges != null) {
            lb_knowledge.setText(String.join(" | ", q.knowledges));
        } else {
            lb_knowledge.setText("");
        }
    }

    public String generateQuestionHtml(String html) {
        String width = "650px";
        String fontSzie = "24px";
        StringBuilder sb = new StringBuilder();
        sb.append("<!DOCTYPE html><html><head><meta charset=\"utf-8\" /><style type=\"text/css\"> .question{ margin:0 auto; width:")
                .append(width)
                .append("; font-size:")
                .append(fontSzie)
                .append("; text-align:left; } </style> </head> <body> <div class=\"question\">")
                .append(html)
                .append("</div></body></html>");
        return sb.toString();
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        mPanel = new JPanel();
        mPanel.setLayout(new BorderLayout(0, 0));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 3, new Insets(4, 4, 4, 4), -1, -1));
        panel1.setBackground(new Color(-4145088));
        mPanel.add(panel1, BorderLayout.SOUTH);
        final com.intellij.uiDesigner.core.Spacer spacer1 = new com.intellij.uiDesigner.core.Spacer();
        panel1.add(spacer1, new com.intellij.uiDesigner.core.GridConstraints(0, 2, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        final JLabel label1 = new JLabel();
        Font label1Font = this.$$$getFont$$$(null, -1, 16, label1.getFont());
        if (label1Font != null) label1.setFont(label1Font);
        label1.setForeground(new Color(-16777216));
        label1.setText("知识点：");
        panel1.add(label1, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        lb_knowledge = new JLabel();
        Font lb_knowledgeFont = this.$$$getFont$$$(null, -1, 16, lb_knowledge.getFont());
        if (lb_knowledgeFont != null) lb_knowledge.setFont(lb_knowledgeFont);
        lb_knowledge.setForeground(new Color(-16777216));
        lb_knowledge.setText("Knowledge");
        panel1.add(lb_knowledge, new com.intellij.uiDesigner.core.GridConstraints(0, 1, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        htmlPane = new JTextPane();
        htmlPane.setContentType("text/html");
        htmlPane.setEditable(false);
        htmlPane.setEnabled(true);
        htmlPane.setMargin(new Insets(16, 3, 3, 3));
        htmlPane.setMaximumSize(new Dimension(950, 1000));
        htmlPane.setMinimumSize(new Dimension(950, 100));
        mPanel.add(htmlPane, BorderLayout.CENTER);
    }

    /**
     * @noinspection ALL
     */
    private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) {
        if (currentFont == null) return null;
        String resultName;
        if (fontName == null) {
            resultName = currentFont.getName();
        } else {
            Font testFont = new Font(fontName, Font.PLAIN, 10);
            if (testFont.canDisplay('a') && testFont.canDisplay('1')) {
                resultName = fontName;
            } else {
                resultName = currentFont.getName();
            }
        }
        Font font = new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
        boolean isMac = System.getProperty("os.name", "").toLowerCase(Locale.ENGLISH).startsWith("mac");
        Font fontWithFallback = isMac ? new Font(font.getFamily(), font.getStyle(), font.getSize()) : new StyleContext().getFont(font.getFamily(), font.getStyle(), font.getSize());
        return fontWithFallback instanceof FontUIResource ? fontWithFallback : new FontUIResource(fontWithFallback);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return mPanel;
    }
}
